package com.itheima.controller;


import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

@Controller
public class AccountController {
    static{
        //放在这里可以
//        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//        AccountService service = (AccountService) ctx.getBean("service的id");

        //TODO: 更好的方案,项目一启动就加载
        //监听ServletContext的监听器  : 创建和销毁
//        ServletContextListener
    }
    @Autowired
    private AccountService service;

//    @ResponseBody //让pojo变成json格式字符串
    @RequestMapping("/find")
    public ModelAndView find(){

        List<Account> list = service.findAll();
        for (Account account : list) {
            System.out.println(account);
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("list");
        mv.addObject("list", list);
       return mv;
    }
}
